package com.chalk.common.util;

import com.chalk.common.constant.ResultConstants;

import java.util.HashMap;

/**
 * @author xiaoqiang
 * @Description 返回结果信息
 * @date 2019/3/21 16:50
 */
public class Result extends HashMap<Object, Object> {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * 成功
     *
     * @return
     */
    public static Result success() {
        Result result = new Result();
        result.put("code", ResultConstants.CallResult.SUCCESS.getCode());
        result.put("msg", ResultConstants.CallResult.SUCCESS.getMsg());
        result.put("data", null);
        return result;
    }

    public static Result success(String msg) {
        Result result = success();
        result.put("msg", msg);
        return result;
    }

    public static Result success(ResultCode code, Object obj) {
        Result result = success();
        result.put("code", code.code());
        result.put("msg", code.msg());
        result.put("data", obj);
        return result;
    }

    public static Result success(Object obj) {
        Result result = success();
        result.put("data", obj);
        return result;
    }

    public static Result success(String msg, Object obj) {
        Result result = success();
        result.put("msg", msg);
        result.put("data", obj);
        return result;
    }

    /**
     * 失败
     *
     * @return
     */
    public static Result fail() {
        Result result = new Result();
        result.put("code", ResultConstants.CallResult.FAIL.getCode());
        result.put("msg", ResultConstants.CallResult.FAIL.getMsg());
        result.put("data", null);
        return result;
    }

    public static Result fail(String msg) {
        Result result = fail();
        result.put("msg", msg);
        return result;
    }

    public static Result fail(ResultCode code, Object obj) {
        Result result = fail();
        result.put("code", code.code());
        result.put("msg", code.msg());
        result.put("data", obj);
        return result;
    }

    public static Result fail(Object obj) {
        Result result = fail();
        result.put("data", obj);
        return result;
    }

    public static Result fail(String msg, Object obj) {
        Result result = fail();
        result.put("msg", msg);
        result.put("data", obj);
        return result;
    }

    /**
     * 异常
     *
     * @return
     */
    public static Result error() {
        Result result = new Result();
        result.put("code", ResultConstants.CallResult.ERROR.getCode());
        result.put("msg", ResultConstants.CallResult.ERROR.getMsg());
        result.put("data", null);
        return result;
    }

    public static Result error(String msg) {
        Result result = error();
        result.put("msg", msg);
        return result;
    }

    public static Result error(Object obj) {
        Result result = error();
        result.put("data", obj);
        return result;
    }

    public static Result error(String msg, Object obj) {
        Result result = error();
        result.put("msg", msg);
        result.put("data", obj);
        return result;
    }

    /**
     * 自定义
     *
     * @param code
     * @param msg
     * @param obj
     * @return
     */
    public static Result custom(Object code, Object msg, Object obj) {
        Result result = new Result();
        result.put("code", code);
        result.put("msg", msg);
        result.put("data", obj);
        return result;
    }

    /**
     *
     * @return
     */
    public static Result notLogin(){
        Result result = new Result();
        result.put("code", ResultCode.NOT_LOGIN.code());
        result.put("msg", ResultCode.NOT_LOGIN.msg());
        result.put("data", null);
        return result;
    }

}
